// pages/appLetWork/work11/index.js
const EarthUtils = require("./Earth");
var fmarkers = [];
var timer = 0;
Page({
  data: {
    second: 0,
    running: false,
    latitude: 0,
    longitude: 0,
    meters: 0,
    markers: [],
    interval: 1000,
    polyline: [{
      points: [],
      color: "#ff0000",
      width: 2,
    }],
    feedbackrate: 500,//回放时间间隔
  },
  onClickLeft() {
    wx.navigateBack({
      delta: 1,
    })
  },
  run() {
    this.setData({
      running: !this.data.running
    })
  },
  clear() {
    this.setData({
      second: 0,
      running: false,
      latitude: 0,
      longitude: 0,
      meters: 0,
      markers: [],
      interval: 10000,
      polyline: [{
        points: [],
        color: "#ff0000",
        width:2
      }],
    })
  },

  save() {
    wx.setStorage({
      data: this.data.markers,
      key: 'running',
    }).then(() => {
      wx.showToast({
        title: '保存成功',
      })
    })
  },

  playBack() {
    this.clear()
    wx.getStorage({
      key: 'running'
    }).then(res => {
      fmarkers = res.data
    })
    timer = setInterval(this.feedback, this.data.feedbackrate)
  },

  feedback() {
    let lmarkers = this.data.markers
    let lpolyline = this.data.polyline
    if (fmarkers.length > 0) {
      lmarkers.push(fmarkers.shift());
      lpolyline[0].points = lmarkers
    } else {
      clearInterval(timer)
    }
    this.setData({
      markers: lmarkers,
      polyline: lpolyline,

    })
  },

  getLocation() {
    wx.getLocation({
      type: 'gcj02'
    }).then(res => {
      console.log(res)
      let { latitude, longitude } = res
      this.setData({
        latitude, longitude
      })
    }).catch(err => {
      console.log(err)
    })
  },
  // test() {
  //   let dis = EarthUtils.getDistance(23.383101,113.449479,23.38703,113.446121)
  //   console.log(dis)
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.test()
    this.getLocation()
    setInterval(this.record, this.data.interval)

  },
  record() {
    if (!this.data.running) {
      return
    }
    this.setData({
      second: this.data.second + this.data.interval / 1000
    })
    wx.getLocation({
      type: 'gcj02'
    }).then(res => {
      let newWarker = {
        latitude: res.latitude,
        longitude: res.longitude,
        iconPath: 'location.png',
        width: 10,
        height: 10
      }
      let pace = 0;
      let tmarkers = this.data.markers
      if (this.data.markers.length > 0) {
        let lastmarker = this.data.markers.slice(-1)[0];
        console.log(lastmarker)
        console.log(newWarker)
        pace = EarthUtils.getDistance(lastmarker.latitude, lastmarker.longitude, newWarker.latitude, newWarker.longitude)
        console.log(pace)
        if (pace > 15) {
          tmarkers.push(newWarker)
        } else {
          pace = 0
        }
      } else {
        tmarkers.push(newWarker)
      }
      this.setData({
        latitude: res.latitude,
        longitude: res.longitude,
        markers: tmarkers,
        meters: this.data.meters + pace
      })
    })
  },
})